﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DCSLibrary;
using Utils.DC.Web.UI;
using Utils.DatabaseUtils;
using Utils.DCSControl;
using Utils.Entity;

public partial class Admin_Form_Product_Add : System.Web.UI.Page
{
    CategoryPageUI objCategoryPage = new CategoryPageUI();
    ProductUI objProduct = new ProductUI();
    DataSet dsCategoryPage;

    protected void Page_Load(object sender, EventArgs e)
    {
        dsCategoryPage = objCategoryPage.GetAll();
        if (!IsPostBack)
        {
            BindCategoryPage();
            if (Request.QueryString["id"] != null && Request.QueryString["id"].Trim() != "")
            {
                try
                {
                    LoadEditData(Convert.ToInt32(Request.QueryString["id"]));
                }
                catch (FormatException fe)
                {
                }
            }
        }
    }

    
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        //bien page-alias
        string alias = Utils.Format.ConvertToNoSign(txtProductName.Text.Trim());
        string folderImage = "";
        if (txtProductAbstract.Text.Trim() != "")
        {
            folderImage = txtProductAbstract.Text.Trim()[txtProductAbstract.Text.Trim().Length - 1].ToString() != "/" ? txtProductAbstract.Text.Trim() + "/" : txtProductAbstract.Text.Trim();
        }

        if (txtProductID.Value.Trim() == "")
        {
            try
            {
                int result = objProduct.Add(Convert.ToInt32(cboCategoryPage.SelectedValue), txtMetaKeyword.Text, txtMetaDescription.Text, txtProductName.Text, txtProductPageTitle.Text, alias, folderImage, txtProductContent.Content, txtProductContent.Text.Trim()
                    , txtProductImage.Text, "", 0, Convert.ToDecimal(txtProductPrice.Text), Convert.ToDecimal(txtDiscountPrice.Text), 0, 0, txtUnit.Text.Trim(), cboType.SelectedValue, "", "", "", 0, Convert.ToInt32(txtViewOrder.Text), chkIsHot.Checked, chkIsTypical.Checked, chkIsNew.Checked, false,
                    false, false, false, Convert.ToByte(cboStatus.SelectedValue), ((UserEntity)Session[PageUtility.SESSION_USER_NAME]).UserID, DateTime.Now, ((UserEntity)Session[PageUtility.SESSION_USER_NAME]).UserID, "", "");

                if (result > 0)
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_ADD_SUCCESSFULLY;
                    ReInsertData();
                }
                else
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
                //lblMessage.Text = MessageBox.MESSAGE_DUPLICATE_DATA;
                lblMessage.Text = ex.ToString();
            }
        }
        else
        {
            try
            {

                int result = objProduct.Update(Convert.ToInt32(txtProductID.Value), Convert.ToInt32(cboCategoryPage.SelectedValue), txtMetaKeyword.Text, txtMetaDescription.Text, txtProductName.Text, txtProductPageTitle.Text, alias, txtProductAbstract.Text.Trim()[txtProductAbstract.Text.Trim().Length - 1].ToString() != "/" ? txtProductAbstract.Text.Trim() + "/" : txtProductAbstract.Text.Trim(), txtProductContent.Content, txtProductContent.Text.Trim(),
                    txtProductImage.Text, "", 0, Convert.ToDecimal(txtProductPrice.Text), Convert.ToDecimal(txtDiscountPrice.Text), 0, 0, txtUnit.Text.Trim(), cboType.SelectedValue, "", "", "", Convert.ToInt32(txtViewOrder.Text),
                    chkIsHot.Checked, chkIsTypical.Checked, chkIsNew.Checked, false, false, false, Convert.ToByte(cboStatus.SelectedValue), "", "");

                if (result > 0)
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_SUCCESSFULLY;
                    ReInsertData();
                }
                else
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
                lblMessage.Text = MessageBox.MESSAGE_CONFILCT_DATA;
            }
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Admin/quan-tri-noi-dung/quan-tri-thong-tin-san-pham.aspx");
    }

    #region Cac ham tuong tac du lieu

    private void BindCategoryPage()
    {
        try
        {
            cboCategoryPage.Items.Clear();

            DataTable dt = new DataTable();
            dt.Columns.Add("FunctionPageID");
            dt.Columns.Add("FunctionPageName");

            DataTable dtParent = TableLib.CreateTable(dsCategoryPage.Tables[0],"ParentID = 4");
            DataTable dtChild = null;
            foreach (DataRow dbrow in dtParent.Rows)
            {
                cboCategoryPage.Items.Add(new ListItem(dbrow["FunctionPageName"].ToString().Trim(),dbrow["FunctionPageID"].ToString().Trim()));
                dtChild = TableLib.CreateTable(dsCategoryPage.Tables[0], "ParentID = " + dbrow["FunctionPageID"].ToString().Trim());
                foreach (DataRow dbrow2 in dtChild.Rows)
                {
                    cboCategoryPage.Items.Add(new ListItem("       -------" + dbrow2["FunctionPageName"].ToString().Trim(), dbrow2["FunctionPageID"].ToString().Trim()));
                }
            }
        }
        catch (Exception e)
        {

        }
    }

    private void LoadEditData(int ProductID)
    {
        DataSet ds = objProduct.GetByID(ProductID);
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            DataRow row = ds.Tables[0].Rows[0];
            txtProductID.Value = row["ProductID"].ToString();

            txtMetaDescription.Text = row["MetaDescription"].ToString();
            txtMetaKeyword.Text = row["MetaKeyword"].ToString();
            txtProductContent.Content = row["ProductContent"].ToString();
            txtProductAbstract.Text = row["ProductAbstract"].ToString();
            txtProductImage.Text = row["ProductImage"].ToString();
            txtProductName.Text = row["ProductName"].ToString();
            txtProductPageTitle.Text = row["ProductPageTitle"].ToString();
            txtProductPrice.Text = row["CurrentPrice"].ToString();
            txtDiscountPrice.Text = row["DiscountPrice"].ToString();
            txtUnit.Text = row["Unit"].ToString();
            txtViewOrder.Text = row["ViewOrder"].ToString();
            chkIsHot.Checked = Convert.ToBoolean(row["IsHot"]);
            chkIsNew.Checked = Convert.ToBoolean(row["IsNew"]);
            chkIsTypical.Checked = Convert.ToBoolean(row["IsTypical"]);
            cboType.SelectedValue = row["PromotionInformation"].ToString(); 
            cboStatus.SelectedValue = row["Status"].ToString();
            General.SetSelectedItemInComboBox(row["CategoryPageID"].ToString(), cboCategoryPage);
            btnCreate.Text = "Cập nhật";
        }
    }

    private void ReInsertData()
    {
        //txtProductID.Value = "";
        txtMetaDescription.Text = "";
        txtMetaKeyword.Text = "";
        txtProductContent.Content = "";
        txtProductAbstract.Text = "";
        txtProductImage.Text = "";
        txtProductName.Text = "";
        txtProductPageTitle.Text = "";
        txtProductPrice.Text = "";
        txtDiscountPrice.Text = "0";
        txtUnit.Text = "";
        txtViewOrder.Text = "";
        chkIsHot.Checked = false;
        chkIsNew.Checked = false;
        chkIsTypical.Checked = false;
        General.SetSelectedItemInComboBox(null, cboCategoryPage);
    }

    #endregion
}
